const mysql_query = require("../common/mysql");
const httpresult = require("../common/response");
const utils = require("../common/utils");
var define = require('../common/define');

async function list(bank_id = []) {
    return await mysql_query("select question.question_id, question.description, question.stem, question.analysis, question.answer, " +
        "chapter.name as chapter, bank.name as bank, bank_question.order_number as order_number, bank_question.bank_id as bank_id, " +
        "bank_question.score as score, bank.course_id as course_id, question.chapter_id as chapter_id from bank_question " +
        "left join question on bank_question.question_id = question.question_id " +
        "left join bank on bank_question.bank_id = bank.bank_id " +
        "left join chapter on question.chapter_id = chapter.chapter_id " +
        "where bank_question.bank_id in (?)", [req.body.bank_ids]);
}

async function get_questions(sql, where_params) {
    var [error, data] = await mysql_query(sql, where_params);
    if (error) {
        return [error, data];
    }
    let questions = [];
    let map = new Map();
    for (let index in data) {
        data[index].description = utils.add_src_host(data[index].description);
        data[index].stem = utils.add_src_host(data[index].stem);
        data[index].analysis = utils.add_src_host(data[index].analysis);
        data[index].answer = utils.add_src_host(data[index].answer);
        data[index].type_name = define.question_type_info[data[index].type];
        // 去重
        if (!map.has(data[index].question_id)) {
            map.set(data[index].question_id, true)
            questions.push(data[index])
        }
    }

    questions = questions.sort((a, b) => {
        return a.order_number - b.order_number;
    });

    return [null, questions];
}
exports.list = list;
exports.get_questions = get_questions;